<template>
  <div class="mod-sp__spmatchitemteam">
    <el-form :inline="true" :model="dataForm" @keyup.enter="queryList()">
      <el-form-item>
        <el-select v-model="dataForm.matchId" placeholder="赛事" clearable filterable @change="getMatchItemList" :disabled="$route.query.id ? true : false">
          <el-option value="">全部</el-option>
          <el-option v-for="item in matchList" :key="item.id" :label="item.matchName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.matchItemId" placeholder="赛项" multiple clearable filterable @change="setMatchItemName" :disabled="$route.query.id ? true : false">
          <el-option value="">全部</el-option>
          <el-option v-for="item in matchItemList" :key="item.id" :label="item.baseMatchItemName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.groupId" placeholder="组别" multiple>
          <el-option value="">全部</el-option>
          <el-option v-for="item in groupsList" :key="item.id" :label="item.dictLabel" :value="item.id"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="地区" prop="province">
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.province" multiple clearable filterable @change="selectCity">
          <el-option value="" label="全国">全国</el-option>
          <el-option v-for="itemO in area.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select v-model="dataForm.type" placeholder="赛项" clearable filterable>
          <el-option v-for="item in typeList.item" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="info" @click="exportDate()">{{ $t("export") }}</el-button>
      </el-form-item>

      <!--

      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatchitemteam:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatchitemteam:delete')" type="danger" @click="deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
      -->
    </el-form>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 发送短信 -->
    <detail v-if="detailVisible" ref="send" @refreshDataList="getDataList"></detail>
    <!-- 弹窗, 代表队 -->
    <TeamDetail v-if="teamVisible" ref="teamRef" @refreshDataList="getDataList"></TeamDetail>
    <!-- 弹窗, 选手 -->
    <userDetail v-if="userVisible" ref="userRef" @refreshDataList="getDataList"></userDetail>
  </div>
  <el-dialog v-model="bohui" title="驳回">
    <!-- 对话框内容  -->
    <el-form-item label="驳回原因">
      <el-input v-model="rejectReason" placeholder=""></el-input>
    </el-form-item>

    <br />
    <br />
    <el-button @click="bohui = false">{{ $t("cancel") }}</el-button>
    <el-button type="primary" @click="submitReject()">{{ $t("confirm") }}</el-button>
    <!-- 对话框内容 -->
  </el-dialog>
  <upload v-if="uploadVisible" ref="upload" uploadurl="/sp/spmatchitemteam/importScore" @refreshDataList="getDataList"></upload>
  <upload v-if="uploadVisible2" ref="upload2" uploadurl="/sp/spmatchitemteam/importPormotion" @refreshDataList="getDataList"></upload>
  <upload v-if="uploadVisible3" ref="upload3" uploadurl="/sp/spmatchitemteam/importCertificate" downloadurl="/sp/spmatchitemteam/exportCertificateError" @refreshDataList="getDataList"></upload>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import AddOrUpdate from "./spmatchitemteam-add-or-update.vue";
import Detail from "./spmatchitemteam-detail.vue";
import TeamDetail from "@/views/sp/spteam-detail.vue";
import UserDetail from "@/views/sp/spuser-detail.vue";
import baseService from "@/service/baseService";
import Upload from "./score-upload.vue";
import { IObject } from "@/types/interface";
import { getDictDataList } from "@/utils/utils";
import { useStore } from "vuex";
export default defineComponent({
  components: {
    AddOrUpdate,
    TeamDetail,
    UserDetail,
    Detail,
    Upload
  },
  setup() {
    const state = reactive({
      bohui: false,
      bohuiId: "",
      rejectReason: "",
      uploadVisible: false,
      uploadVisible2: false,
      uploadVisible3: false,
      getDataListURL: "/sp/spmatchitemteam/signRecord",
      createdIsNeed: false,
      activatedIsNeed: false,
      getDataListIsPage: true,
      exportURL: "/sp/spmatchitemteam/exportSignRecord",
      deleteURL: "/sp/spmatchitemteam",
      deleteIsBatch: true,
      dataForm: {
        type: "1",
        matchId: "-1",
        matchName: "",
        item: "",
        matchItemId: "",
        teamId: "",
        teamName: "",
        isRise: "",
        status: "0",
        realName: "",
        groupName: [],
        province: [],
        city: "",
        groupId: "",
        company: "",
        ucompany: ""
      },
      matchItem: {
        match_name: "",
        item_name: ""
      },
      activeName: "first",
      userVisible: false,
      teamVisible: false,
      detailVisible: false,
      detailId: "",
      area: { code: "0", item: [{ code: "0", name: "请选择" }] },
      areaCity: { code: "0", item: [{ code: "0", name: "请选择" }] },
      typeList: {
        code: "1",
        item: [
          { code: "1", name: "模式1" },
          { code: "2", name: "模式2" }
        ]
      },
      groupsList: [],
      matchList: [],
      matchItemList: [] as IObject[]
    });
    return { ...useView(state), ...toRefs(state) };
  },
  watch: {},
  mounted() {
    //获取上个页面传来的参数
    this.dataForm.matchItemId = this.$route.query.id ? this.$route.query.id + "" : "";
    this.dataForm.matchId = this.$route.query.matchId ? this.$route.query.matchId + "" : "";
    if (this.dataForm.matchItemId) {
      this.queryBymatchItemId(this.dataForm.matchItemId);
      this.getMatchItemList();
    }
    if (this.dataForm.matchId) {
      this.query();
    }
  },
  created() {
    this.selectMatch();
    this.getGroupsList();
    this.areaList(this.area);
  },
  methods: {
    queryList() {
      if (this.dataForm.matchId) {
        this.query();
      }
    },

    /**
     * 查询所有赛事
     */
    selectMatch() {
      baseService.get("/sp/spmatch/list").then((res) => {
        if (res.code == 0) {
          this.matchList = res.data;
        }
      });
    },

    /**
     * 查询赛项
     */
    getMatchItemList() {
      baseService.get("/sp/spmatchitem/list?matchId=" + this.dataForm.matchId).then((res) => {
        if (res.code == 0) {
          this.matchItemList = res.data;
        }
      });
    },

    /**
     * 设置赛项名称
     */
    setMatchItemName() {
      let matchItem = this.matchItemList.find((item) => item.id === this.dataForm.matchItemId);
      debugger;
      console.log(matchItem);
      this.matchItem.match_name = matchItem.matchName;
      this.matchItem.item_name = matchItem.baseMatchItemName;
    },

    detailHandle(row: IObject) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.send.dataForm.row = row;
        this.$refs.send.init();
      });
    },
    uploadScore() {
      this.uploadVisible = true;
      this.$nextTick(() => {
        this.$refs.upload.init();
      });
    },
    uploadPormotion() {
      this.uploadVisible2 = true;
      this.$nextTick(() => {
        this.$refs.upload2.init();
      });
    },
    uploadCertificate() {
      this.uploadVisible3 = true;
      this.$nextTick(() => {
        this.$refs.upload3.init();
      });
    },
    // 批量审核通过
    passBatchHandle() {
      // 循环取dataListSelections的id
      let ids: string[] = [];
      for (let i = 0; i < this.dataListSelections!.length; i++) {
        let id = this.dataListSelections![i].id;
        let status = this.dataListSelections![i].status;
        if (status !== 0) {
          return this.$message.error("请选择待审核的数据");
        }
        ids.push(this.dataListSelections![i].id);
      }
      if (ids.length === 0) {
        return this.$message.error("请选择要审核的数据");
      }

      this.$confirm("确认审核通过？", this.$t("prompt.title"), {
        confirmButtonText: this.$t("confirm"),
        cancelButtonText: this.$t("cancel"),
        type: "warning"
      }).then(() => {
        baseService.post("/sp/spmatchitemteam/passBatch", ids).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            this.query();
          }
        });
      });
    },
    handleClick(tab: any, event: any) {
      if (tab.props.name === "first") {
        this.activeName = "first";
        this.dataForm.status = "0";
        this.getDataListURL = "/sp/spmatchitemteam/signRecord";
      } else if (tab.props.name === "second") {
        this.activeName = "second";
        this.dataForm.status = "-1";
        this.getDataListURL = "/sp/spmatchitemteam/signRecord";
      } else if (tab.props.name === "three") {
        this.activeName = "three";
        this.dataForm.status = null;
        this.getDataListURL = "/sp/spmatchitemteam/signRecordRise";
      }
      if (this.dataForm.matchId) {
        this.query();
      }
    },
    exportDate() {
      this.exportURL = "/sp/spmatchReport/export?1=1";
      if (!this.dataForm.matchId) {
        //提示不能为空
        return this.$message.error("请选择赛事");
      }
      if (this.dataForm.type == "2") {
        if (this.dataForm.matchItemId.length != 1) {
          //提示不能为空
          return this.$message.error("请选择一条赛项");
        }
      }
      this.exportURL += "&myMatchId=" + this.dataForm.matchId;
      if (this.dataForm.matchItemId.length > 0) {
        this.exportURL += "&mymatchItemId=" + this.dataForm.matchItemId;
      }
      if (this.dataForm.groupId.length > 0) {
        this.exportURL += "&mygroupId=" + this.dataForm.groupId;
      }
      if (this.dataForm.province.length > 0) {
        this.exportURL += "&myprovince=" + this.dataForm.province.join();
      }
      this.exportURL += "&type=" + this.dataForm.type;
      this.exportURL += "&aa=" + 2;
      console.log(this.dataForm);

      this.exportHandle();
    },
    /**
     * 重置成报名成功
     */
    updateStatusHandle() {
      // 循环取dataListSelections的id
      let ids: string[] = [];
      for (let i = 0; i < this.dataListSelections!.length; i++) {
        ids.push(this.dataListSelections![i].id);
      }
      if (ids.length === 0) {
        return this.$message.error("请选择要重置的数据");
      }
      this.$confirm("确认重置成报名成功？", this.$t("prompt.title"), {
        confirmButtonText: this.$t("confirm"),
        cancelButtonText: this.$t("cancel"),
        type: "warning"
      }).then(() => {
        baseService.post("/sp/spmatchitemteam/updateStatus", { id: ids[0], status: "1" }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            this.query();
          }
        });
      });
    },

    /**
     * 查询score配置
     * @param matchItemId
     */
    queryBymatchItemId(matchItemId: any) {
      baseService.post("/sp/spmatchitem/queryBymatchItemId", { id: matchItemId }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        } else {
          console.log(res.data);
          this.matchItem = res.data;
        }
      });
    },

    // 获取省份
    areaList(target: any) {
      baseService.get("/front/fast/sys/areaDict/list?pid=" + target.code).then((e: any) => {
        target.item = e.data;
      });
    },
    // 选择城市
    selectCity() {
      if (this.dataForm.province) {
        this.areaCity.code = this.dataForm.province + "";
        this.areaList(this.areaCity);
      } else {
        this.dataForm.city = "";
        this.areaCity.item = [];
      }
    },
    getGroupsList() {
      const store = useStore();
      this.groupsList = getDictDataList(store.state.dicts, "groups");
    },
    teamHandle(id?: string) {
      this.teamVisible = true;
      baseService.get("/sp/spteam/" + id).then((e: any) => {
        this.$nextTick(() => {
          if (e.data) {
            e.data.sex = e.data.sex == 0 ? "女" : "男";
          }
          this.$refs.teamRef.dataForm.row = e.data;
          this.$refs.teamRef.init();
        });
      });
    },
    userHandle(id?: string) {
      this.userVisible = true;
      baseService.get("/sp/spuser/" + id).then((e: any) => {
        this.$nextTick(() => {
          if (e.data) {
            e.data.sex = e.data.sex == 0 ? "女" : "男";
          }
          console.log(this.$refs.userRef.dataForm);
          this.$refs.userRef.dataForm.row = e.data;
          this.$refs.userRef.init();
        });
      });
    },
    //提交拒绝
    submitReject() {
      baseService.post("/sp/spmatchitemteam/pass", { id: this.bohuiId, status: "8", rejectReason: this.rejectReason }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        } else {
          this.query();
        }
      });
      this.bohui = false;
    }
  }
});
</script>
